{{> page_top}}
<style>

    .content {
        padding: 15px 0;
    }

    div.content > ul > li {
        margin-bottom: 15px;
        box-shadow: rgba(0, 0, 0, 0.1) 1px 2px 5px 1px;
        overflow: hidden;
        border-radius: 4px;
        padding: 15px;
        border-color: rgb(238, 238, 238);
        border-width: 1px;
        border-style: solid;
        background: #6b6659 linear-gradient(to bottom, #a29466 0%, #6b6659 100%);

    }

    div.content > ul > li > h2 {
        display: block;
        font-size: 1.5em;
        margin-block-start: 0;
        margin-block-end: 0.83em;
        margin-inline-start: 0;
        margin-inline-end: 0;
        font-weight: bold;
    }

    div.content button {
        font-size: 0.6em;
        line-height: 1.5em;
        border: 0 none;
        background: #bada55;
        background: linear-gradient(to bottom, #bada55 0%, #8ca440 100%);
        cursor: pointer;
    }

    p.rule {
        margin-top: 15px;
        color: rgb(0, 0, 0);
        line-height: 2;
        padding: 5px 15px;
        background: rgb(238, 238, 238);
        border-radius: 4px;
    }


    section.verification > label {
        display: flex;

    }

    section.verification > label > textarea {
        font-size: 1em;
        line-height: 1.5em;
        border: 0 none;
        outline: none;
        background: #cbcbba;
        padding: 0 0.5em;
        margin: 15px 0 0.25em;
        width: 100% !important;
        box-sizing: border-box;
        font-family: Consolas, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace;
    }


    p.error {
        color: red;
        font-weight: bold;
        font-size: 1em;
    }

    p.success {
        font-size: 1em;
        color: green;
        font-weight: bold;
    }
</style>
<div class="application">

    <textarea id="searchInput" type="text" oninput="searchReg(this)" placeholder="搜索关键词, 如'手机'"></textarea>
</div>
<div class="content">
    <ul>
        <li>
            <h2>
                火车车次
            </h2>
            <p class="rule">
                <button class="copy-rule" onclick="copyRule(this)">点击复制</button>
                <button class="graphic" onclick="graphic(this)">可视化展示</button>
                <code readonly class="javascript">/^[GCDZTSPKXLY1-9]\d{1,4}$/</code>
            </p>
            <section class="verification">
                <label>
                    <textarea type="text" oninput="verification(this,'/^[GCDZTSPKXLY1-9]\d{1,4}$/')"
                              placeholder="例如: G1868, D102, D9, Z5, Z24, Z17"></textarea>
                    <button class="btn-clear" onclick="verificationInputClear(this)">
                        清空
                    </button>
                </label>
                <div class="tip">
                    <p hidden class="error">不通过</p>
                    <p hidden class="success">通过</p>
                </div>
            </section>
        </li>

    </ul>
</div>
<script>

    function htmlEncode(str) {
        if (typeof str !== 'string') {
            str = str + ''
        }
        let s = "";
        if (str.length === 0) return "";
        s = str.replace(/&/g, "&amp;");
        s = s.replace(/</g, "&lt;");
        s = s.replace(/>/g, "&gt;");
        s = s.replace(/ /g, "&nbsp;");
        s = s.replace(/\'/g, "&#39;");
        s = s.replace(/\"/g, "&quot;");
        s = s.replace(/\n/g, "<br/>");
        return s;
    }

    function loadData(data) {
        let lisHtml = '';
        for (const i in data) {
            const item = data[i]
            // }
            // for (const item of data) {
            console.info(item)
            let arr = [`例如: ${item.examples.join(', ')}`];
            if (undefined !== item.counterExamples) {
                arr.push(`反例: ${item.counterExamples.join(', ')}`);
            }

            let placeholder = htmlEncode(arr.join(' , '))
            let ruleHtml = htmlEncode(item.rule)
            let itemHtml = `<li>
            <h2>
                ${item.title}
<button class="copy-rule" onclick="copyRule(this)">点击复制</button>
<button class="graphic" onclick="graphic(this)">图形展示</button>
<button class="btn-clear" onclick="verificationInputClear(this)">清空测试内容</button>
            </h2>
            <p class="rule">
                <code readonly class="javascript">${ruleHtml}</code>
            </p>
            <section class="verification">
                <label>
                    <textarea type="text" oninput="verification(this,${i})"
                           placeholder="${placeholder}"></textarea>
                </label>
                <div class="tip">
                    <p hidden class="error">不通过</p>
                    <p hidden class="success">通过</p>
                </div>
            </section>
        </li>`
            lisHtml += itemHtml;
        }
        let ulEl = document.querySelector('.content ul')
        ulEl.innerHTML = lisHtml
    }
</script>
<script type="module">
    import rules from './js/RULES.js'

    let ruleHash = {};
    for (const arrKey in rules) {
        const rule = rules[arrKey]
        ruleHash[arrKey] = rule
    }
    window.rules = ruleHash;
    console.info('ruleHash', ruleHash)
    loadData(ruleHash)
</script>
<script>

    function graphic(el) {
        let res = encodeURI(el.parentElement.parentElement.querySelector('code').textContent);
        location = "/#" + res
    }

    function copyRule(el) {
        el.textContent = '复制成功'
        setTimeout(() => el.textContent = '点击复制', 1000)
        return navigator.clipboard.writeText(el.parentElement.parentElement.querySelector('code').textContent).catch(err => {
            throw (err !== undefined ? err : new DOMException('The request is not allowed', 'NotAllowedError'))
        })
    }

    function verification(el, regi) {
        let errorEl = el.parentElement.parentElement.querySelector('.tip>.error')
        let successEl = el.parentElement.parentElement.querySelector('.tip>.success')
        let regex = window.rules[regi].rule
        let testText = el.value;
        let result = regex.test(testText);
        if (result) {
            errorEl.hidden = true
            successEl.hidden = false
        } else {
            errorEl.hidden = false
            successEl.hidden = true
        }
    }

    function verificationInputClear(el) {
        el.parentElement.parentElement.querySelector('textarea').value = ''
    }


    function searchReg(el) {
        let search = el.value
        let ruleHash = {};
        for (const arrKey in window.rules) {
            const rule = window.rules[arrKey]
            if (rule.title.indexOf(search) >= 0) {
                ruleHash[arrKey] = rule
            }
        }

        loadData(ruleHash)
    }


</script>


{{> page_bottom}}
